{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_zh = '../data/news-commentary/aligned/Russian-Chinese/Chinese/'\n",
    "dir_ru = '../data/news-commentary/aligned/Russian-Chinese/Russian/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "files = os.listdir(dir_ru)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../data/news-commentary/all.zh', 'w') as fo_zh, open('../data/news-commentary/all.ru', 'w') as fo_ru:\n",
    "    for file_name in files:\n",
    "        path_ru = dir_ru + '/' + file_name\n",
    "        path_zh = dir_zh + '/' + file_name\n",
    "        texts_zh = []\n",
    "        texts_ru = []\n",
    "        with open(path_zh, 'r') as f_zh, open(path_ru, 'r') as f_ru:\n",
    "            par_zh = []\n",
    "            par_ru = []\n",
    "            for line_zh, line_ru in zip(f_zh, f_ru):\n",
    "                line_zh = line_zh.strip()\n",
    "                line_ru = line_ru.strip()\n",
    "                if line_ru.startswith('<AUTHOR '):\n",
    "                    continue\n",
    "                if line_ru == '<P>' and line_zh == '<P>':\n",
    "                    texts_zh.append(' '.join([x for x in par_zh if x]))\n",
    "                    texts_ru.append(' '.join([x for x in par_ru if x]))\n",
    "                    par_zh = []\n",
    "                    par_ru = []\n",
    "                else:\n",
    "                    par_zh.append(line_zh)\n",
    "                    par_ru.append(line_ru)\n",
    "            if par_zh:\n",
    "                texts_zh.append(' '.join([x for x in par_zh if x]))\n",
    "                texts_ru.append(' '.join([x for x in par_ru if x]))\n",
    "        for line_zh, line_ru in zip(texts_zh, texts_ru):\n",
    "            print(line_zh, file=fo_zh)\n",
    "            print(line_ru, file=fo_ru)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
